/******************************************************************************
 *                  Shanghai ChipON Micro-Electronic Co.,Ltd                  *
 ******************************************************************************
 *  $File Name$       : main.c                                         	      *
 *  $Author$          : ChipON AE/FAE Group                                   *
 *  $Data$            : 2021-07-8                                             *
 *  $Hard Version     : KF32A156-MINI-EVB_V1.2                                *
 *  $Description$     : Show how to use external or internal clock and output *
 *  					clock through special pin							  *
 ******************************************************************************
 *  Copyright (C) by Shanghai ChipON Micro-Electronic Co.,Ltd                 *
 *  All rights reserved.                                                      *
 *                                                                            *
 *  This software is copyright protected and proprietary to                    *
 *  Shanghai ChipON Micro-Electronic Co.,Ltd.                                 *
 ******************************************************************************
 *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  *
 *                     		REVISON HISTORY                               	  *
 *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  *
 *  Data       Version  Author        Description                             *
 *  ~~~~~~~~~~ ~~~~~~~~ ~~~~~~~~~~~~  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  *
 *  2021-07-08 00.02.00 FAE Group     Version 2.0 update                      *
 *                                                                            *
 *                                                                            *
 *****************************************************************************/

/******************************************************************************
**                      		Include Files                                **
******************************************************************************/
#include "system_init.h"
#include "Usart.h"
/*******************************************************************************
**                      	Private Macro Definitions                         **
*******************************************************************************/
/**
 * @brief Select the type of clock output from the OSC_OUT pins.In the following
 * line choose the out type
 * 	CLKOUT_SCLK		----  select main system clock
 * 	CLKOUT_EXTLF	----  select External low frequency crystal oscillator
 * 	CLKOUT_EXTHF	----  select External high frequency crystal oscillator
 * 	CLKOUT_INTLF	----  select Internal low frequency crystal oscillator
 * 	CLKOUT_INTHF	----  select Internal high frequency crystal oscillator
 * 	CLKOUT_PLL		----  select PLL
 *  CLKOUT_LP4M  	----  select LP4M
 * */
#if !defined  (CLK_OUT_TYPE)
  #define CLK_OUT_TYPE    	(CLKOUT_SCLK)
#endif /* HSI_STARTUP_TIMEOUT */

/**
 *  @brief :Initialize the LED GPIO ports
 * 	PC6 ------------- LED2
 *
 *  @param in :None
 *  @param out :None
 *  @retval :None
 */
void BoardGpioInit(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;

	/* Configure PC6 LED2 output */
	GPIO_InitStructure.m_Mode = GPIO_MODE_OUT;
	GPIO_InitStructure.m_OpenDrain = GPIO_POD_PP;
	GPIO_InitStructure.m_PullDown = GPIO_NOPULL;
	GPIO_InitStructure.m_PullUp = GPIO_NOPULL;
	GPIO_InitStructure.m_Speed = GPIO_LOW_SPEED;
	GPIO_InitStructure.m_Pin = GPIO_PIN_MASK_6;
	GPIO_Configuration(GPIOC_SFR , &GPIO_InitStructure);


}
/*******************************************************************************
**                     			main Functions 		             	     	  **
*******************************************************************************/
int main()
{
	/* Initialize the system clock */
	SystemInit(SYSCLK_FREQ);
	/* Setup SysTick Timer as delay function */
	systick_delay_init(SYSCLK_FREQ);
	BoardGpioInit();
	/* Enale LP4M */
	OSC_LP4M_Software_Enable(TRUE);
	while(OSC_Get_LP4MIF_INT_Flag() != SET);
	/* Configure PA0 remap to SCLKOUT */
	GPIO_Write_Mode_Bits(GPIOA_SFR, GPIO_PIN_MASK_0, GPIO_MODE_RMP);
	GPIO_Pin_RMP_Config(GPIOA_SFR, GPIO_Pin_Num_0, GPIO_RMP_PG1_AF0);

	/* Select the type of clock output from the OSC_OUT pins !!! */
	OSC_SCLK_Output_Select(CLKOUT_LP4M);
	/* Enable SCLKOUT, and frequency division is <1:16> */
	OSC_SCLK_Output_Division_Config(CLKOUT_DIVISION_1);
	OSC_SCLK_Output_Enable(TRUE);
	while(1)
	{

		GPIO_Toggle_Output_Data_Config (GPIOC_SFR,GPIO_PIN_MASK_6);
		systick_delay_ms(500);
	}		
}

/**
 *  @brief : Reports the name of the source file and the source line number
 *           where the assert_param error has occurred.
 *  @param in :	file pointer to the source file name
 * 	@param in :	line assert_param error line source number
 *  @param out :None
 *  @retval :None
 */
void check_failed(uint8_t* File, uint32_t Line)
{
	/* User can add his own implementation to report the file name and line number,
		ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

	/* Infinite loop */
	while(1)
	{
		;
	}
};
